'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 用户
 * @author sundbcn@gmail.com (sun debing)
 */

var moment = require('moment');
var mongoose = require('mongoose');
var dataApi = require('../libs/dataApi');
var constantUtil = require('../util/constantUtil');

/**
 * 用户
 */
var userSchema = new mongoose.Schema({
    // 玩家id
    uid: {
        type: Number,
        required: true,
        unique: true,
        index: true
    },

    // 上次登陆的角色类型
    last_login_job_type: {
        type: Number,
        default: 0
    },

    forbid: {
        type: Boolean,
        default: false
    },

    // 开始封停时间
    start_time: {
        type: Date,
        default: 0
    },

    end_time: {
        type: Date,
        default: 0
    },

    reason: {
        type: String,
        default: ''
    },

    // 注册时间
    create_time: {
        type: Date,
        default: Date.now
    },

    // 最后登入时间
    last_login_time: {
        type: Date,
        default: Date.now
    }
});

/**
 * 获取用户id
 * @return {[type]} [description]
 */
userSchema.methods.getUid = function () {
    return this.uid;
};

/**
 * 设置上次登陆的角色类型
 * @param {[type]} job_type [description]
 */
userSchema.methods.setLastLoginJobType = function (job_type) {
    this.last_login_job_type = job_type;
};

/**
 * 获取上次登陆的角色类型
 * @return {[type]} [description]
 */
userSchema.methods.getLastLoginJobType = function () {
    return this.last_login_job_type;
};

/**
 * 封用户
 * @param  {[type]} start_time [description]
 * @param  {[type]} end_time   [description]
 * @param  {[type]} reason     [description]
 * @return {[type]}            [description]
 */
userSchema.methods.setForbid = function (start_time, end_time, reason) {
    this.forbid = true;
    this.start_time = start_time;
    this.end_time = end_time;
    this.reason = reason;
};

/**
 * 解封
 */
userSchema.methods.unForbid = function () {
    this.forbid = false;
    this.start_time = 0;
    this.end_time = 0;
    this.reason = '';
};

/**
 * 是否被封停
 */
userSchema.methods.isForbid = function () {
    return this.forbid;
};

/**
 * 结束封停时间
 * @return {[type]} [description]
 */
userSchema.methods.getEndTime = function () {
    return this.end_time;
};

/**
 * 获取上次登陆的时间
 */
userSchema.methods.getLastLoginTime = function () {
    return this.last_login_time;
};

if (!userSchema.options.toJSON) {
    userSchema.options.toJSON = {};
}
/* jshint unused:false */
userSchema.options.toJSON.transform = function (doc, ret) {
    delete ret.last_login_time;
    delete ret._id;
    delete ret.__v;
};

mongoose.model('User', userSchema);
